﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		BlogController.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using System.Linq;
using System.Net.Mail;
using System.Web.Mvc;
using Dexter.Domain.Model;
using Dexter.Logger.Contracts;
using Dexter.Search;
using Dexter.Services;
using Dexter.Web.Mvc.Filters;
using Dexter.Web.Mvc.Results;
using Dexter.Web.Mvc.ViewModel.FrontEnd.Binders;
using Dexter.Web.Mvc.ViewModel.FrontEnd.BlogController;
using Dexter.Web.Syndication.Contracts;
using Dexter.Web.Utils;

namespace Dexter.Web.Mvc.Controllers.FrontEnd {
	public class BlogController : PublicControllerBase {
		readonly IRssService rssService;

		/// <summary>
		/// Initializes a new instance of the <see cref="BlogController"/> class.
		/// </summary>
		/// <param name="searchService">The search service.</param>
		/// <param name="commentService">The comment service.</param>
		/// <param name="referrerService">The referrer service.</param>
		/// <param name="configurationService">The configuration service.</param>
		/// <param name="webUtils">The web utils.</param>
		/// <param name="logger">The logger.</param>
		/// <param name="urlBuilder">The URL builder.</param>
		/// <param name="blogRollService">The blog roll service.</param>
		/// <param name="categoryService">The category service.</param>
		/// <param name="postService">The post service.</param>
		/// <param name="themeManager">The theme manager.</param>
		/// <param name="tagService">The tag service.</param>
		/// <param name="pageService">The page service.</param>
		/// <param name="pluginHost">The plugin host.</param>
		/// <param name="rssService">The RSS service.</param>
		public BlogController ( ISearchService searchService , ICommentService commentService , IReferrerService referrerService , IConfigurationService configurationService , IWebUtils webUtils , ILogger logger , IUrlBuilder urlBuilder , IBlogRollService blogRollService , ICategoryService categoryService , IPostService postService , IThemeManager themeManager , ITagService tagService , IPageService pageService , IRssService rssService ) : base ( searchService , commentService , referrerService , configurationService , webUtils , logger , urlBuilder , blogRollService , categoryService , postService , themeManager , tagService , pageService) {
			this.rssService = rssService;
		}

		[PingBack]
		[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
		[DexterCallFilter(true)]
		public ActionResult Post ( string id ) {
			if ( string.IsNullOrEmpty ( id ) ) {
				return HttpNotFound ( );
			}

			var post = base.PostService.GetPostDtoBySlug( id );

			if ( post == null ) {
				return HttpNotFound ( );
			}

			var data = base.GetViewData ( new BlogIndexViewData ( base.WebUtils , base.Configuration , base.UrlBuilder ) );
			data.Post = post;
			data.ReleatedPost = base.SearchService.ReleatedPost ( post.Id , 0 , 5 );

			data.SetTitle ( post.Title );
			data.SetMetaKeywords ( post.Tags );
			data.SetDescription ( post.Abstract );

			return View ( data );
		}

		[AcceptVerbs(HttpVerbs.Post)]
		[ValidateAntiForgeryToken]
		[DexterCallFilter ( false )]
		public ActionResult Post ( string id, CommentBinder comment ) {
			if (string.IsNullOrEmpty ( id )) {
				return HttpNotFound ( );
			}

			var post = base.PostService.GetPostDtoBySlug ( id );

			if (post == null) {
				return HttpNotFound ( );
			}

			if (!ModelState.IsValid || !post.CommentEnabled) {
				var data = base.GetViewData ( new BlogIndexViewData ( base.WebUtils, base.Configuration, base.UrlBuilder ) );
				data.Post = post;
				data.ReleatedPost = base.SearchService.ReleatedPost ( post.Id, 0, 5 );

				data.SetTitle ( post.Title );
				data.SetMetaKeywords ( post.Tags);
				data.SetDescription ( post.Abstract );

				return View ( data );
			}

			Uri url = null;

			if(!comment.Url.IsNullOrEmpty (  ))
				url = new Uri ( comment.Url );

			CommentService.AddComment ( comment.Comment, comment.Author, new MailAddress ( comment.Email ), url, post.Id, null );

			return Redirect ( UrlBuilder.Post.Detail ( post ) );
		}

		[PingBack]
		[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
		[DexterCallFilter ( true )]
		public ActionResult Print ( string id ) {
			if ( string.IsNullOrEmpty ( id ) ) {
				return HttpNotFound ( );
			}

			var post = base.PostService.GetPostDtoBySlug ( id );

			if ( post == null ) {
				return HttpNotFound ( );
			}

			var data = base.GetViewData ( new BlogIndexViewData ( base.WebUtils , base.Configuration , base.UrlBuilder ) );
			data.Post = post;

			data.SetTitle ( "Printable version of post " + post.Title , true );
			data.SetMetaKeywords ( post.Tags);
			data.SetDescription ( post.Abstract );

			return View ( data );
		}

		[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
		[DexterCallFilter ( true )]
		public ActionResult Related ( string id , int? page ) {
			if ( string.IsNullOrEmpty ( id ) ) {
				return HttpNotFound ( );
			}

			var paretPost = PostService.GetBySlug ( id );

			if ( paretPost == null ) {
				return HttpNotFound ( );
			}

			int pageIndex = page ?? 0;

			var data = base.GetViewData ( new BlogRelatedViewData ( base.WebUtils , base.Configuration , base.UrlBuilder ) );

			data.ParentPost = paretPost.ToDto();
			data.RelatedContent = SearchService.ReleatedPost ( data.ParentPost.Id, pageIndex, 30);

			data.SetMetaKeywords ( base.GetKeywordsFromSearchResult ( data.RelatedContent.Result ) );

			return View ( data );
		}

		[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
		[DexterCallFilter ( true )]
		public ActionResult Rss ( string id ) {
			if ( string.IsNullOrEmpty ( id ) ) {
				return HttpNotFound ( );
			}

			Post post = base.PostService.GetBySlug ( id );

			if ( post == null ) {
				return HttpNotFound ( );
			}

			return new RssResult ( base.Configuration , post.Comments.ToFlat ( x => x.Comments ).ToArray ( ) , this.rssService , base.UrlBuilder );
		}

		[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
		[DexterCallFilter ( true )]
		public ActionResult Archive ( int? year , int? month , int? page ) {
			int pageIndex = page ?? 0;

			if ( year == null || month == null ) {
				Logger.WarnFormat ( "Possible wrong link : {0} - The referref is {1}" , HttpContext.Request.Url , HttpContext.Request.UrlReferrer );

				return HttpNotFound ( );
			}

			if ( year < 1900 || month < 1 ) {
				return HttpNotFound ( );
			}

			var data = base.GetViewData ( new BlogArchiveViewData ( base.WebUtils , base.Configuration , base.UrlBuilder ) );
			data.Posts = PostService.GetByMonth ( month.Value , year.Value , pageIndex , base.Configuration.ElementInListsConfiguration.NumberOfElementsInList );

			return View ( data );
		}
	}
}
